const toString = Object.prototype.toString

function is(val: unknown, type: string): boolean {
  // 使用Object.prototype.toString获取准确类型，格式为 "[object Type]"
  return Object.prototype.toString.call(val) === `[object ${type}]`
}

export const isObject = (val: any): val is Record<any, any> => {
  return val !== null && is(val, 'Object')
}

export function isDate(val: unknown): val is Date {
  return is(val, 'Date')
}

export function isNumber(val: unknown): val is number {
  return is(val, 'Number')
}

export function isString(val: unknown): val is string {
  return is(val, 'String')
}

export const isFunction = (val: unknown): val is () => void =>
  typeof val === 'function'

export function isBoolean(val: unknown): val is boolean {
  return is(val, 'Boolean')
}

export function isArray(val: any): val is Array<any> {
  return val && Array.isArray(val)
}

// export const isMobile = (): boolean => {
//   return !!navigator.userAgent.match(
//     /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|
// 	  wOSBrowser|BrowserNG||WebOS|Symbian|Windows Phone)/i
//   )
// }

export const isUrl = (path: string): boolean => {
  const reg =
    /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
  return reg.test(path)
}

export const isImgPath = (path: string): boolean => {
  if (path.includes('picsum.photos') || path.includes('loremflickr.com')) {
    return true
  }
  return /(https?:\/\/|data:image\/).*?\.(png|jpg|jpeg|gif|svg|webp|ico)/gi.test(
    path
  )
}

export const isEmptyVal = (val: any): boolean => {
  return val === '' || val === null || val === undefined
}
